﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ObviousCode.UI.Aspect.PluginLibrary.Interfaces;
using ObviousCode.Common.Utils.Library.Dictionaries;
using ObviousCode.Aspect.PluginLibrary.Interfaces;

namespace ObviousCode.UI.Aspect.Persistence.AspectSurface.Savers
{
    public class SurfaceSaver_V1 : SurfaceSaver
    {
        protected override void PerformSave(IAspectSurface surface)
        {            
            SaveObjects(surface);
            SaveRelationships(surface);
        }

        private void SaveRelationships(IAspectSurface surface)
        {
            PropertyArray surfaceRelationships = new PropertyArray();

            Dictionary.Set("surface-relationships", surfaceRelationships);

            foreach (ISurfaceRelationship relationship in surface.Collection.Relationships)
            {
                surfaceRelationships.Append(relationship.InstanceKey);
            }
        }

        private void SaveObjects(IAspectSurface surface)
        {
            PropertyArray surfaceObjects = new PropertyArray();

            Dictionary.Set("surface-objects", surfaceObjects);

            foreach (ISurfaceObject item in surface.Collection.Items)
            {
                surfaceObjects.Append(item.InstanceKey);
            }
        }

        public override int Version
        {
            get { return 1; }
        }
    }
}
